﻿using LBSoft.Business;
using LBSoft.Business.DAL;
using LBSoft.Common.DotNetBean;
using LBSoft.Common.DotNetCode;
using LBSoft.Common.DotNetData;
using LBSoft.Common.DotNetEncrypt;
using LBSoft.Common.DotNetExt;
using LBSoft.Common.DotNetJson;
using LBSoft.Common.DotNetUI;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI.WebControls;


namespace LBSoft.HplusAdmin.Business.Index
{
    /// <summary>
    /// Index 的摘要说明
    /// </summary>
    public class Index : IHttpHandler
    {
        HttpContext con = null;
        mJsonResult json = new mJsonResult();
        Common_Dal systemidao = new Common_Dal();
        Permission_Dal permissionDal = new Permission_Dal();
        LogHelper log = new LogHelper();

        public void ProcessRequest(HttpContext context)
        {
            con = context;

            switch (con.Request["PostType"])
            {
                case "get": GetData(); break;
                case "set": SetData(); break;
            }
        }

        #region 获取
        private void GetData()
        {
            switch (con.Request["ActionType"])
            {
                case "LoginUserInfo": GetLoginUserInfo(); break; //获取登录用户信息
            }
        }

        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        private void GetLoginUserInfo()
        {
            string userId = RequestSession.GetSessionUser().UserId;
            Hashtable ht = systemidao.GetDataByID("SYS_UserInfo", "GUID", userId);
            ht.Remove("UserPwd");

            con.Response.Write(JsonConvert.SerializeObject(ht));
        }
        #endregion

        #region  编辑
        private void SetData()
        {
            switch (con.Request["ActionType"])
            {
                case "ChangeUserPwd": SetChangeUserPwd(); break; //修改密码
            }
        }

        /// <summary>
        /// 字典保存
        /// </summary>
        private void SetChangeUserPwd()
        {
            try
            {
                string userId = RequestSession.GetSessionUser().UserId;
                string userAccount = RequestSession.GetSessionUser().UserAccount;

                string oldUserPwd = con.Request["OldUserPwd"].ToString2();
                string newUserPwd = con.Request["NewUserPwd"].ToString2();

                UserInfo_Dal userInfo_Dal = new UserInfo_Dal();
                DataTable dtlogin = userInfo_Dal.UserLogin(userAccount.Trim(), oldUserPwd.Trim());
                if (dtlogin == null || dtlogin.Rows.Count == 0)
                {
                    json.success = false;
                    json.msg = "密码修改失败，原密码有误！";
                    con.Response.Write(JsonConvert.SerializeObject(json));
                    return;
                }
                Hashtable htUser = new Hashtable();
                htUser.Add("UserPwd", Md5Helper.MD5(newUserPwd, 32));

                bool result = systemidao.SubmitData("SYS_UserInfo", "GUID", userId, htUser);
                json.success = result;

                if (result)
                {
                    json.msg = "修改成功！";
                }
                else
                {
                    json.msg = "修改失败！";
                }
            }
            catch (Exception ex)
            {
                json.msg = ex.Message;
                log.WriteExceptionLog(ex);
            }

            con.Response.Write(JsonConvert.SerializeObject(json));
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}